﻿Imports System.Data.SqlClient

Public Class frmStaffProfilesRegistry

    Private Sub FillBranches()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Branches As ArrayList = GetBranches()

            For Each Branch As String In Branches
                Me.CombBranch.Items.Add(Branch)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub CheckFilter()
        If Me.RAll.Checked Then
            Me.txtName.Clear()
            Me.txtName.Enabled = False
            Me.CombBranch.SelectedIndex = -1
            Me.CombBranch.Enabled = False

        ElseIf Me.RByName.Checked Then
            Me.txtName.Clear()
            Me.txtName.Enabled = True
            Me.CombBranch.SelectedIndex = -1
            Me.CombBranch.Enabled = False

        ElseIf Me.RByBranch.Checked Then
            Me.txtName.Clear()
            Me.txtName.Enabled = False
            Me.CombBranch.SelectedIndex = -1
            Me.CombBranch.Enabled = True
        End If
    End Sub

    Sub FillStaffList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Cond As String = ""

            If Me.RStatusOnDuty.Checked = True Then
                Cond = "Where EndOfServiceDate Is Null "

            ElseIf Me.RStatusReleased.Checked = True Then
                Cond = "Where EndOfServiceDate Is Not Null "
            Else
                Cond = ""
            End If

            If Me.RByName.Checked Then
                If Cond.Trim.Length = 0 Then
                    Cond += "Where FullNameEn Like N'%" & Me.txtName.Text.Trim & "%'"
                Else
                    Cond += "and FullNameEn Like N'%" & Me.txtName.Text.Trim & "%'"
                End If

            ElseIf Me.RByBranch.Checked Then
                If Cond.Trim.Length = 0 Then
                    Cond += "Where Branch Like N'%" & Me.CombBranch.Text.Trim & "%'"
                Else
                    Cond += "and Branch Like N'%" & Me.CombBranch.Text.Trim & "%'"
                End If

            ElseIf RAll.Checked Then
                Cond += ""
            End If

            Dim cmd As New SqlCommand("Select EmpNo,FullNameEn,Branch,Position,Nationality,HireDate,EndOfServiceDate From StaffProfiles " & Cond, cnn)
            Dim Reader As SqlDataReader
            Dim EndOfServiceDate As String

            Me.GridStaffProfiles.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                If Reader.Item("EndOfServiceDate") Is DBNull.Value Then
                    EndOfServiceDate = ""
                Else
                    EndOfServiceDate = CDate(Reader.Item("EndOfServiceDate")).ToString("yyyy/MM/dd")
                End If

                Me.GridStaffProfiles.Rows.Add(New String() {Reader.Item("EmpNo"), Reader.Item("FullNameEn"), Reader.Item("Branch"), _
                                                            Reader.Item("Position"), Reader.Item("Nationality"), _
                                                            CDate(Reader.Item("HireDate")).ToString("yyyy/MM/dd"), EndOfServiceDate})
            End While

            cnn.Close()

            'Set Colours
            For Each row As DataGridViewRow In Me.GridStaffProfiles.Rows
                If row.Cells(Me.DGColEndOfServiceDate.Index).Value <> "" Then
                    row.Cells(Me.DGColEndOfServiceDate.Index).Style.BackColor = Color.Salmon
                End If
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PrintEmployee(ByVal EmpID As Integer)
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim dap As New SqlDataAdapter(" Select * From StaffProfiles where EmpNo= " & EmpID, cnn)
            Dim dt As New DataTable
            dap.Fill(dt)

            Dim rpt As New StaffProfile
            rpt.SetDataSource(dt)
            rptViewer.CrystalReportViewer1.ReportSource = rpt
            rptViewer.CrystalReportViewer1.RefreshReport()
            rptViewer.ShowDialog()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillBranches()

        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridStaffProfiles.CellClick
        If e.ColumnIndex = DGColEdit.Index Then
            Dim a As New frmStaffProfilesEdit
            a.FillPositionsList()
            a.FillNationalityList()
            a.FillBranches()
            a.FillReligionList()
            a.txtEEmpNo.Text = GridStaffProfiles.Rows(e.RowIndex).Cells(0).Value
            a.FillEmployeeDetails()
            a.ShowDialog()
            FillStaffList()

        ElseIf e.ColumnIndex = Me.DGColPrintProfile.Index Then
            PrintEmployee(CInt(Me.GridStaffProfiles.CurrentRow.Cells("Column1").Value))

        ElseIf e.ColumnIndex = Me.DGColDocuments.Index Then
            Dim a As New frmStaffProfilesDocuments
            a.txtStaffID.Text = Me.GridStaffProfiles.CurrentRow.Cells(0).Value
            a.txtStaffName.Text = Me.GridStaffProfiles.CurrentRow.Cells(1).Value
            a.FillDocuments()
            a.ShowDialog()

        ElseIf e.ColumnIndex = Me.Dependents.Index Then
            Dim a As New frmDependent
            a.txtEmpNo.Text = Me.GridStaffProfiles.CurrentRow.Cells(0).Value
            a.TxtName.Text = Me.GridStaffProfiles.CurrentRow.Cells(1).Value
            a.txtBranch.Text = Me.GridStaffProfiles.CurrentRow.Cells(2).Value
            a.txtPosition.Text = Me.GridStaffProfiles.CurrentRow.Cells(3).Value
            a.txtNationality.Text = Me.GridStaffProfiles.CurrentRow.Cells(4).Value
            a.ShowDialog()

            FillStaffList()

        ElseIf e.ColumnIndex = Me.DGColEndOfService.Index Then
            Dim a As New frmStaffProfilesEndOfService
            a.txtEEmpNo.Text = GridStaffProfiles.Rows(e.RowIndex).Cells(0).Value
            If Me.GridStaffProfiles.CurrentRow.Cells(Me.DGColEndOfServiceDate.Index).Value = "" Then
                a.Ended = False
            Else
                a.Ended = True
            End If
            a.FillEmployeeDetails()
            a.ShowDialog()

            FillStaffList()
        End If
    End Sub

    Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridStaffProfiles.DoubleClick

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim a As New frmStaffProfilesNew
        a.ShowDialog()

        FillStaffList()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        FillStaffList()
    End Sub

    Private Sub RByName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RByName.CheckedChanged
        CheckFilter()
    End Sub

    Private Sub RByBranch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RByBranch.CheckedChanged
        CheckFilter()
    End Sub

    Private Sub RAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RAll.CheckedChanged
        CheckFilter()
    End Sub

    Private Sub btnPrintAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintAll.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Cond As String
            If Me.RByName.Checked Then
                Cond = "Where FullNameEn Like N'%" & Me.txtName.Text.Trim & "%'"

            ElseIf Me.RByBranch.Checked Then
                Cond = "Where Branch Like N'%" & Me.CombBranch.Text & "%'"

            ElseIf Me.RAll.Checked Then
                Cond = ""
            End If

            Dim dap As New SqlDataAdapter("Select * From StaffProfiles " & Cond, cnn)
            Dim dt As New DataTable
            dap.Fill(dt)

            Dim rpt As New StaffProfilesList
            rpt.SetDataSource(dt)
            rptViewer.CrystalReportViewer1.ReportSource = rpt
            rptViewer.CrystalReportViewer1.RefreshReport()
            rptViewer.ShowDialog()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Sub CheckStatus()
        If Me.RStatusOnDuty.Checked = True Then
            Me.DGColEndOfServiceDate.Visible = False
        Else
            Me.DGColEndOfServiceDate.Visible = True
        End If
    End Sub

    Private Sub RStatusOnDuty_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RStatusOnDuty.CheckedChanged
        CheckStatus()
        FillStaffList()
    End Sub

    Private Sub RStatusReleased_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RStatusReleased.CheckedChanged
        CheckStatus()
        FillStaffList()
    End Sub

    Private Sub RStatusAll_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RStatusAll.CheckedChanged
        CheckStatus()
        FillStaffList()
    End Sub

    Private Sub btnPrintBlankProfile_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintBlankProfile.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim rpt As New StaffProfile
            rptViewer.CrystalReportViewer1.ReportSource = rpt
            rptViewer.ShowDialog()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class